package com.xhj.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.xhj.config.AliyunVODProperties;
import com.xhj.service.VODService;
import com.xhj.util.VODUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class VODServiceImpl implements VODService {

    @Autowired
    private AliyunVODProperties vodProperties;

    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename  = file.getOriginalFilename();
            assert originalFilename != null;
            String title = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret(), title, originalFilename, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = response.getVideoId();
            if (!response.isSuccess()){
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                log.warn(errorMessage);
                if (StringUtils.isEmpty(videoId)) throw new RuntimeException(errorMessage);
            }
            return videoId;
        } catch (Exception e) {
            throw new RuntimeException("上传视频失败！");
        }
    }

    @Override
    public void deleteVideoById(String videoId) {
        try {
            DefaultAcsClient client = VODUtils.initVodClient(vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("RequestId=" + response.getRequestId());
        } catch (ClientException e) {
            throw new RuntimeException("视频删除失败！");
        }
    }

    @Override
    public void deleteVideoList(List<String> videoIdList) {
        if (videoIdList == null || videoIdList.size() < 1) throw new RuntimeException("该课程云端无视频，不需要删除！");
        try {
            DefaultAcsClient client = VODUtils.initVodClient(vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");
            request.setVideoIds(videoIds);
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("RequestId=" + response.getRequestId());
        } catch (ClientException e) {
            throw new RuntimeException("视频删除失败！");
        }
    }

    @Override
    public String getPlayAuth(String videoId) {
        try {
            GetVideoPlayAuthResponse response = VODUtils.getVideoPlayAuth(vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret(), videoId);
            return response.getPlayAuth();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
